home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / asa / asa.h < prev    next >
C/C++ Source or Header  |  1994-10-23  |  8KB  |  297 lines

  1.  /* $Id: asa.h,v 4.2 1994/10/23 23:35:19 ingber Exp ingber $ */
  2.  
  3.  /* asa.h for Adaptive Simulated Annealing */
  4.  
  5. #include "asa_user.h"
  6.  
  7. #define ZERO            ((double) 0.0)
  8. #define ONE            ((double) 1.0)
  9. #define TWO            ((double) 2.0)
  10. #define TEN            ((double) 10.0)
  11. #define HALF            ((double) 0.5)
  12.  
  13. #define NORMAL_EXIT            0
  14. #define P_TEMP_TOO_SMALL        1
  15. #define C_TEMP_TOO_SMALL        2
  16. #define COST_REPEATING            3
  17. #define TOO_MANY_INVALID_STATES        4
  18.  
  19. #ifndef TIME_STD
  20. #define TIME_STD FALSE
  21. #endif
  22.  
  23. #if TIME_CALC
  24. #if TIME_STD
  25. #include <sys/syscall.h>
  26. #endif
  27. #include <sys/time.h>
  28. #include <sys/resource.h>
  29. #endif
  30.  
  31.  /* Set this to TRUE to override the P_TEMP_TOO_SMALL test */
  32. #ifndef NO_PARAM_TEMP_TEST
  33. #define NO_PARAM_TEMP_TEST FALSE
  34. #endif
  35.  
  36.  /* Set this to TRUE to override the C_TEMP_TOO_SMALL test */
  37. #ifndef NO_COST_TEMP_TEST
  38. #define NO_COST_TEMP_TEST FALSE
  39. #endif
  40.  
  41.  /* Printing Options */
  42.  
  43. #ifndef ASA_PRINT
  44. #define ASA_PRINT TRUE
  45. #endif
  46.  
  47. #if ASA_PRINT
  48. #else
  49. #if ASA_SAMPLE
  50. #define ASA_PRINT TRUE
  51. #endif
  52. #endif
  53.  
  54. #ifndef ASA_OUT
  55. #define ASA_OUT "asa_out"
  56. #endif
  57.  
  58.  /* You can set ASA_PRINT_INTERMED to TRUE to print out
  59.     intermediate data when SELF_OPTIMIZE is set to TRUE */
  60. #ifndef ASA_PRINT_INTERMED
  61. #if SELF_OPTIMIZE
  62. #define ASA_PRINT_INTERMED FALSE
  63. #else
  64. #define ASA_PRINT_INTERMED TRUE
  65. #endif
  66. #endif
  67.  
  68. #ifndef ASA_PRINT_MORE
  69. #define ASA_PRINT_MORE FALSE
  70. #endif
  71.  
  72.  /* The state of the system in terms of parameters and function value */
  73. typedef struct
  74.   {
  75.     double cost;
  76.     double *parameter;
  77.   }
  78. STATE;
  79.  
  80.  /* essential MACROS */
  81.  
  82. #define MIN(x,y)    ((x) < (y) ? (x) : (y))
  83. #define MAX(x,y)    ((x) > (y) ? (x) : (y))
  84.  
  85. #if USER_REANNEAL_FUNCTION
  86. #else
  87.  /* REANNEAL_FUNCTION(temperature, tangent, max_tangent)
  88.     determines the reannealed temperature. */
  89. #define REANNEAL_FUNCTION(temperature, tangent, max_tangent) \
  90.  (temperature * (max_tangent / tangent))
  91. #endif
  92.  
  93.  /* IABS(i)
  94.     absolute value for integers, in stdlib.h on _some_ machines */
  95. #define IABS(i) ((i) < 0? -(i) : (i))
  96.  
  97.  /*  NO_REANNEAL(x)
  98.     can determine whether to calculate derivatives. */
  99. #define NO_REANNEAL(x)    (IABS(parameter_type[x]) == 2)
  100.  
  101.  /* VFOR
  102.     is a simple macro to iterate on each parameter index. */
  103.  
  104. #define VFOR(index_v) \
  105.  for (index_v = 0; index_v < *number_parameters; ++index_v)
  106.  
  107.  /* EXPONENT_CHECK
  108.     checks that an exponent x is within a valid range and,
  109.     if not, adjusts its magnitude to fit in the range. */
  110. #define EXPONENT_CHECK(x) \
  111.  ((x) < MIN_EXPONENT ? MIN_EXPONENT : \
  112.  ((x) > MAX_EXPONENT ? MAX_EXPONENT : (x)))
  113.  
  114.  /* PARAMETER_RANGE_TOO_SMALL(x)
  115.     checks if the range of parameter x is too small to work with.
  116.     If user_cost_function changes the parameter ranges,
  117.     this test could be used to adaptively bypass
  118.     some parameters, e.g., depending on constraints. */
  119. #define PARAMETER_RANGE_TOO_SMALL(x) \
  120.  (fabs(parameter_minimum[x] - parameter_maximum[x]) < (double) EPS_DOUBLE)
  121.  
  122.  /* INTEGER_PARAMETER(x)
  123.     determines if the parameter is an integer type. */
  124. #define INTEGER_PARAMETER(x) (parameter_type[x] > 0)
  125.  
  126.  /* ROW_COL_INDEX(i, j)
  127.     converts from row i, column j to an index. */
  128. #define ROW_COL_INDEX(i, j) ((i) + *number_parameters * (j))
  129.  
  130. #if HAVE_ANSI
  131.  
  132.  /* asa function prototypes */
  133. void accept_new_state (double (*user_random_generator) (void),
  134.                double *parameter_minimum,
  135.                double *parameter_maximum,
  136.                double *current_cost_temperature,
  137. #if ASA_SAMPLE
  138.                double *current_user_parameter_temp,
  139. #endif
  140.                ALLOC_INT * number_parameters,
  141.                LONG_INT * recent_number_acceptances,
  142.                LONG_INT * number_accepted,
  143.                LONG_INT * index_cost_acceptances,
  144.                LONG_INT * number_acceptances_saved,
  145.                LONG_INT * recent_number_generated,
  146.                LONG_INT * number_generated,
  147.                LONG_INT * index_parameter_generations,
  148.                STATE * current_generated_state,
  149.                STATE * last_saved_state,
  150. #if ASA_SAMPLE
  151.                FILE * ptr_asa_out,
  152. #endif
  153.                USER_DEFINES * OPTIONS);
  154.  
  155. void generate_new_state (double (*user_random_generator) (void),
  156.              double *parameter_minimum,
  157.              double *parameter_maximum,
  158.              double *current_parameter_temperature,
  159.              ALLOC_INT * number_parameters,
  160.              int *parameter_type,
  161.              STATE * current_generated_state,
  162.              STATE * last_saved_state,
  163.              USER_DEFINES * OPTIONS);
  164.  
  165. void reanneal (double *parameter_minimum,
  166.            double *parameter_maximum,
  167.            double *tangents,
  168.            double *maximum_tangent,
  169.            double *current_cost_temperature,
  170.            double *initial_cost_temperature,
  171.            double *temperature_scale_cost,
  172.            double *current_user_parameter_temp,
  173.            double *initial_user_parameter_temp,
  174.            double *temperature_scale_parameters,
  175.            double *quench_param,
  176.            double *quench_cost,
  177.            ALLOC_INT * number_parameters,
  178.            int *parameter_type,
  179.            LONG_INT * index_cost_acceptances,
  180.            LONG_INT * index_parameter_generations,
  181.            STATE * last_saved_state,
  182.            STATE * best_generated_state,
  183.            USER_DEFINES * OPTIONS);
  184.  
  185. void cost_derivatives (double (*user_cost_function) (
  186.                double *, double *, double *, double *, double *,
  187.               ALLOC_INT *, int *, int *, int *, USER_DEFINES *),
  188.                double *parameter_minimum,
  189.                double *parameter_maximum,
  190.                double *tangents,
  191.                double *curvature,
  192.                double *maximum_tangent,
  193.                double *delta_parameter,
  194.                ALLOC_INT * number_parameters,
  195.                int *parameter_type,
  196.                int *exit_status,
  197.                int *curvature_flag,
  198.                int *valid_state_generated_flag,
  199.                LONG_INT * number_invalid_generated_states,
  200.                STATE * current_generated_state,
  201.                STATE * best_generated_state,
  202.                FILE * ptr_asa_out,
  203.                USER_DEFINES * OPTIONS);
  204.  
  205. double generate_asa_state (double (*user_random_generator) (void),
  206.                double *temp);
  207.  
  208. void asa_exit (double (*user_cost_function) (
  209.                double *, double *, double *, double *, double *,
  210.               ALLOC_INT *, int *, int *, int *, USER_DEFINES *),
  211.            double *final_cost,
  212.            double *parameter_initial_final,
  213.            double *parameter_minimum,
  214.            double *parameter_maximum,
  215.            double *tangents,
  216.            double *curvature,
  217.            double *maximum_tangent,
  218.            double *delta_parameter,
  219.            double *current_cost_temperature,
  220.            double *initial_user_parameter_temp,
  221.            double *current_user_parameter_temp,
  222.            double *accepted_to_generated_ratio,
  223.            ALLOC_INT * number_parameters,
  224.            int *parameter_type,
  225.            int *valid_state_generated_flag,
  226.            int *exit_status,
  227.            ALLOC_INT * index_exit_v,
  228.            ALLOC_INT * start_sequence,
  229.            LONG_INT * number_accepted,
  230.            LONG_INT * best_number_accepted_saved,
  231.            LONG_INT * index_cost_acceptances,
  232.            LONG_INT * number_generated,
  233.            LONG_INT * number_invalid_generated_states,
  234.            LONG_INT * index_parameter_generations,
  235.            LONG_INT * best_number_generated_saved,
  236.            STATE * current_generated_state,
  237.            STATE * last_saved_state,
  238.            STATE * best_generated_state,
  239.            FILE * ptr_asa_out,
  240.            USER_DEFINES * OPTIONS);
  241.  
  242. #if ASA_PRINT
  243. void print_state (double *parameter_minimum,
  244.           double *parameter_maximum,
  245.           double *tangents,
  246.           double *curvature,
  247.           double *current_cost_temperature,
  248.           double *current_user_parameter_temp,
  249.           double *accepted_to_generated_ratio,
  250.           ALLOC_INT * number_parameters,
  251.           int *curvature_flag,
  252.           LONG_INT * number_accepted,
  253.           LONG_INT * index_cost_acceptances,
  254.           LONG_INT * number_generated,
  255.           LONG_INT * number_invalid_generated_states,
  256.           STATE * last_saved_state,
  257.           STATE * best_generated_state,
  258.           FILE * ptr_asa_out,
  259.           USER_DEFINES * OPTIONS);
  260.  
  261. void print_asa_options (FILE * ptr_asa_out, USER_DEFINES * OPTIONS);
  262.  
  263. #if TIME_CALC
  264. void aux_print_time (struct timeval *time, char *message,
  265.              FILE * ptr_asa_out);
  266. #if TIME_STD
  267. int syscall (int sys_option, int who, struct rusage *usage);
  268. #else
  269. int getrusage (int who, struct rusage *usage);
  270. #endif /* TIME_CALC */
  271. #endif /* TIME_STD */
  272. #endif /* ASA_PRINT */
  273.  
  274. #else /* HAVE_ANSI */
  275.  
  276. void accept_new_state ();
  277. void generate_new_state ();
  278. void reanneal ();
  279. void cost_derivatives ();
  280. double generate_asa_state ();
  281. void asa_exit ();
  282. #if ASA_PRINT
  283. void print_state ();
  284. void print_asa_options ();
  285.  
  286. #if TIME_CALC
  287. void aux_print_time ();
  288. #if TIME_STD
  289. int syscall ();
  290. #else
  291. int getrusage ();
  292. #endif /* TIME_STD */
  293. #endif /* TIME_CALC */
  294. #endif /* ASA_PRINT */
  295.  
  296. #endif /* HAVE_ANSI */
  297.